<template>
  <!--公司周报管理-->
    <div>
        <div class="search-wrap d-flex a-center">
            <public-search
                    :fields="fields"
                    :search-param="form"
                    @on-query="onQuery"
                    @on-reset="onReset"
            />
            <el-divider class="m-b10" direction="vertical" />
            <el-button size="small" plain class="button_edit m-r10 m-b10" @click="addHandle">
                <svg-icon icon-class="add-icon-1" class="m-r5" />添加
            </el-button>
            <el-button
                    size="small"
                    plain
                    class="button_edit  m-b10"
                    @click="onExport2()"
            >
                <svg-icon icon-class="export" class="m-r5" />
                一键导出
            </el-button>
        </div>

        <public-table
                index
                :page="page"
                :tableData="tableData"
                :tableOption.sync="tableOption"
                @page-change="onPageChange"
        >
            <template #buttons="scope">
                <el-button
                        size="small"
                        class="button_edit"
                        type="primary"
                        @click="toPath(scope.row,0)"
                >查看</el-button>
                <el-button
                        v-if="scope.row.status===1"
                        size="small"
                        class="button_funtion2"
                        type="primary"
                        @click="toPath(scope.row,1)"
                >填写</el-button>
                <el-button
                        v-if="scope.row.status===-1"
                        size="small"
                        class="button_funtion2"
                        type="primary"
                        @click="updateWeeklyStatus(scope.row)"
                >下发</el-button>
            </template>
        </public-table>
    </div>

    <view-popover ref="viewPopoverRef" @refreshList="onQuery"/>
</template>

<script setup>
import PublicTable from "@/components/PublicTable.vue";
import {onMounted, reactive, ref} from "vue";
import {tableCommonFun} from "@/hooks/tableCommon";
import PublicSearch from "@/components/PublicSearch.vue";
import {otherApi, weeklyApi} from "@/api/investment";
import {department} from "@/api/project";
import {useRouter} from "vue-router";
import {ElButton, ElDivider, ElMessage} from "element-plus";
import ViewPopover from "./components/viewPopover.vue";
import {exportExcelFile} from "@/common/utils/GlobalUtils";

const interfaceConfig = reactive({
    fileName:'investment',
    apiPath:'weeklyApi.getWeeklyPage',
});
const exportConfig = reactive({
    fileName:'businessFinance',
    apiPath:'culturalCreativityApi.exportCultural',
    tableName:'文创合同表'
});

const {
    form,
    page,
    tableOption,
    tableData,
    onQuery,
    onReset,
    onPageChange,
    getData,
    onExport
}  = tableCommonFun(interfaceConfig,exportConfig);

form.value = {
    startTime:'',
    endTime:'',
    title:'',
}

tableOption.value = [
    { label: "周报编号 ", prop: "weeklyId", minWidth: 100 },
    { label: "周报名称", prop: "title", minWidth: 100 },
    { label: "填报部门数", prop: "completeCount", minWidth: 100 },
    { label: "开始时间", prop: "startTime", minWidth: 120 },
    { label: "结束时间", prop: "endTime", minWidth: 120 },
    { label: "创建时间", prop: "createTime", minWidth: 120 },
    { label: "创建用户名称", prop: "createUserName", minWidth: 120 },
    {
        label: "状态 ", prop: "status", formatter: (val) => {
            return { '-1': '未下发', '1': '已下发' }[val];
        }, minWidth: 100
    },
    {
        label: '操作',
        prop: 'buttons',
        slot: true,
        width: 200,
        fixed: "right"
    }
];

const fields = ref([
    {   type:'input',
        label:'标题',
        key:'title',
        placeholder:'请输入标题',
        clearable:true
    },
    {   type:'datetimerange',
        label:'周报填写时间',
        key:'valueTime',
        startTimeName:'startTime',
        endTimeName:'endTime',
        placeholder:'请选择周报填写时间',
        clearable:true,
        fun:(e,params)=>{
            if(e && e instanceof Array && e.length>0){
                form.value[params.startTimeName] = e[0];
                form.value[params.endTimeName] = e[1];
            }
            if(!e || (e instanceof  Array && e.length===0)){
                form.value[params.startTimeName] = '';
                form.value[params.endTimeName] = '';
            }
        }
    },
]);

/**
 * 获取所有部门
 */
const getAllDepartments = () => {
    department.getAllDepartments().then((res) => {
        const { code, data, msg } = res;
        if (code == 200) {
            fields.value[3].options.list = data;
        }
    });
};

/**
 * 根据部门id查负责人
 * @param responsibleDepartmentId
 */
const getLeaderByDepartmentId = (responsibleDepartmentId) => {
    department.getLeaderByDepartmentId(responsibleDepartmentId).then((res) => {
        const { code, data, msg } = res;
        if (code == 200) {
            fields.value[4].options.list = data;
        }
    });
};

/**
 * 新增周报
 */
const viewPopoverRef = ref(null);
const addHandle = () => {
    viewPopoverRef.value.open({ type: '建设部' ,isAdd:true})
}

/**
 * 下发周报
 * @param row
 */
const updateWeeklyStatus = (row) => {
    if(row.status===1){
        return ElMessage.warning('已下发不能重复下发!');
    }
    weeklyApi.updateWeeklyStatus({weeklyId:row.weeklyId}).then(res=>{
        if(res.code==='200'){
            ElMessage.success('下发成功!');
            onQuery();
        }
    });
}

/**
 * 周报填写
 * @param row
 */
const router = useRouter();
const toPath = (row,type) => {
    router.push({path:`/workWeeklyReport/companyWeeklyReport/weeklyReportFilling/${row.weeklyId}/${type}`});
}

/**
 * 导出周报
 */
const onExport2 = () => {
    weeklyApi.companyWeeklyExport({startTime:form.value.startTime,endTime:form.value.endTime}).then(res=>{
        exportExcelFile(res, '公司周报');
    });
}

onMounted(()=>{
    getData({
        pageNum:1,
        pageSize:10,
        param:{}
    });
});
</script>

<style scoped>

</style>
